package com.caine.ts.ch18;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import com.caine_ts.R;

public class Lesson18 extends Activity {

	private ImageView img_show = null;
	int[] images = new int[] { R.drawable.one, R.drawable.two,
			R.drawable.three, R.drawable.four, R.drawable.ip4 };
	private int index = 0;
	private Timer timer = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lesson18);
		init();

	}

	
	void init() {
		img_show = (ImageView) findViewById(R.id.img_showImg);
		img_show.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.setClass(Lesson18.this, Lesson18_1.class);
				startActivity(intent);
			}
		});
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 0x111) {
				System.out.println("index : " + index);
				img_show.setImageResource(images[index++]);
				if (index == 5) {
					index = 0;
				}
			}
		};
	};

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		timer.cancel();
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		timer.cancel();
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		timer = new Timer();
		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Message message = new Message();
				message.what = 0x111;
				System.out
						.println("init() : handler.sendMessageDelayed(message, 1000);");
				handler.sendMessageDelayed(message, 1000);
			}
		}, 0, 1000);
	}

}
